<template>
  <div class="container">
    <div class="search-box">
      <el-input
        v-model="taskName"
        placeholder="请输入内容"
        class="input-box"
      ></el-input>
      <el-button @click="clearInput">清除</el-button>
      <el-button @click="searchTask" type="primary">搜索</el-button>
    </div>
    <el-table border style="width: 100%" :data="list">
      <el-table-column
        type="index"
        :index="editIndex"
        label="任务编号"
        width="80"
      >
      </el-table-column>
      <el-table-column prop="name" label="任务名称"> </el-table-column>
      <el-table-column prop="des" label="任务描述"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="180">
        <template #default="{ row }">
          <el-button
            @click="editForm(row.id)"
            size="mini"
            style="margin-right: 10px"
            >编辑</el-button
          >
          <el-popconfirm
            confirmButtonText="好的"
            cancelButtonText="不用了"
            icon="el-icon-info"
            iconColor="red"
            title="确定删除任务吗？"
            @confirm="onConfirm(row.id)"
          >
            <template #reference>
              <el-button size="mini" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="pageContainer">
      <el-pagination
        @current-change="changePage"
        :page-size="params._limit"
        :current-page="params._page"
        layout="prev, pager, next"
        :total="total"
      >
      </el-pagination>
      <el-button @click="addTasklist" size="small" plain>+ 新增</el-button>
    </div>
    <!-- 弹框 -->
    <el-dialog
      title="提示"
      :visible="dialogVisible"
      width="30%"
      @close="cancelDialog"
    >
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
      >
        <el-form-item label="任务名称" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="任务描述" prop="des">
          <el-input v-model.number="ruleForm.des"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="cancelDialog">取消</el-button>
          <el-button @click.prevent="confirm" type="primary">确定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  getListAPI,
  confirmAPI,
  editFormAPI,
  eidtConfirmAPI,
  delTaskAPI,
  searchTaskAPI,
} from "@/api/task.js";
import { v4 as uuidv4 } from "uuid";
export default {
  data() {
    const checkName = async (rule, value, callback) => {
      const res = await getListAPI({
        _page: 1, // 当前第几页
        _limit: 100, // 每页条数
      });
      let arr = res.data;
      let nameList = [];
      if (this.ruleForm.id) {
        arr.forEach((item) => {
          if (item.name !== this.isName) {
            nameList.push(item.name);
          }
        });
      } else {
        arr.forEach((item) => {
          nameList.push(item.name);
        });
      }
      if (nameList.includes(value)) {
        callback(new Error("任务名称已存在"));
      } else {
        callback();
      }
    };
    return {
      isName: "",
      taskName: "",
      list: [],
      params: {
        _page: 1, // 当前第几页
        _limit: 5, // 每页条数
      },
      total: 0,
      dialogVisible: false,
      ruleForm: {
        name: "",
        des: "",
        id: "",
      },
      rules: {
        name: [
          { required: true, message: "名称必填", trigger: "blur" },
          { validator: checkName, trigger: "blur" },
        ],
        des: [{ required: true, message: "名称必填", trigger: "blur" }],
      },
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    async getList() {
      const res = await getListAPI(this.params);
      this.list = res.data;
      this.total = Number(res.headers["x-total-count"]);
    },
    resetForm() {
      this.ruleForm = {
        name: "",
        des: "",
        id: "",
      };
      this.$refs.ruleForm.resetFields();
    },
    addTasklist() {
      this.dialogVisible = true;
    },
    cancelDialog() {
      this.dialogVisible = false;
      this.resetForm();
    },
    confirm() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (!valid) return;
        if (this.ruleForm.id) {
          await eidtConfirmAPI(this.ruleForm);
          this.$message.success("编辑成功");
        } else {
          this.ruleForm.id = uuidv4();
          await confirmAPI(this.ruleForm);
          this.$message.success("新增成功");
          this.params._page = 1;
        }
        this.cancelDialog();
        this.getList();
      });
    },
    async editForm(id) {
      this.dialogVisible = true;
      const res = await editFormAPI(id);
      this.ruleForm = res.data;
      this.isName = res.data.name;
    },
    async onConfirm(id) {
      await delTaskAPI(id);
      this.getList();
    },
    async searchTask() {
      const res = await searchTaskAPI(this.taskName);
      this.list = res.data;
    },
    clearInput() {
      this.taskName = "";
      this.getList();
    },
    changePage(page) {
      this.params._page = page;
      this.getList();
    },
    editIndex(index) {
      return index + (this.params._page - 1) * this.params._limit + 1;
    },
  },
};
</script>

<style scoped lang="less">
.container {
  width: 800px;
  margin: 0 auto;
}
.search-box {
  display: flex;
  margin-bottom: 10px;
  .input-box {
    margin-right: 20px;
  }
}
.pageContainer {
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
}
</style>
